<?php
namespace Admin\Model;
use Think\Model;

class ProjectsummaryModel extends AbstractModel {

	protected $results = array();
	
	public $_validate = array(
        array('ProjectID','number','请选择项目名称',Model::MUST_VALIDATE,"function",1)
    );
	
	/*
	protected $_auto = array(
    	array('ProjectNO', 'getNowNO',3,'callback')
  	);
    */
	
	public function Myupadd(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->add();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "添加成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "添加错误！";
			}
		}
		return $this->results;
	}
	
	
	public function Myupdata(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->save();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "修改成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "修改错误！";
			}
		}
		return $this->results;
	}
	
	
	public function Myshow($parmArr){
		return $this->field("*")->WHERE($parmArr)->find();
	}
	
	public function MyshowList(){
		$where = "";
		$inid = "";
		$keyword = I('keyword');
        if(!empty($keyword)){
			$res = M("project")->field("ID")->where(array("Name"=>array('like','%'.$keyword.'%')))->select();
			foreach($res AS $k => $v){
				$inid .= $v['ID'].",";
			}
			if(!empty($inid)){
				$where['ProjectID'] = array('IN',substr($inid, 0, -1));
			}else{
				$where['ProjectID'] = "-1";
			}
		}
		$return = array();
		$count = $this->where($where)->count();
	    $page = $this->Mypage($count, 8);
	    $fields = " a.*, b.Name AS ProjectName ";
	    $join = " LEFT JOIN project AS b ON a.ProjectID = b.ID ";
		$res = $this->alias('a')->join($join)->where($where)->field($fields)->limit($page->firstRow.','.$page->listRows)->select();
		$return['list'] = $res;
		$return['page'] = $page->show('Admin');
		return $return;
	}
	
	
	public function getProjectList(){
		$depat = M("project");
		//$where['IsDel'] = 0;
		return $depat->field('ID,Name')->where()->select();
	}
	
	public function getTableData(){
		$dataArr = array();
		$laborArr = array();
		$comArr = array();
		$member = " ";
		$i = 0;
		$j = 0;
		$projectCycle = "";
		$join2 =  " LEFT JOIN user AS b ON a.ChargeUserID = b.ID";
		$res2 = M("project")->alias('a')->field("a.Intro,a.Name,a.ImportantPoint,a.StartDate,a.EndDate,b.UserName")->join($join2)->where(array("a.ID"=>$_GET['id']))->find();
		if(!empty($res2['StartDate']) && !empty($res2['EndDate'])){
			$strtotime = (strtotime($res2['EndDate']) - strtotime($res2['StartDate']));
			$projectCycle = ((int)$strtotime / (60*60*24)) >= 0 ? (int)$strtotime / (60*60*24) : 0;
		}
		$res3 = M("project_user_rel as a")->field("b.UserName")->join("LEFT JOIN user AS b ON a.UserID = b.ID")->where(array("a.ProjectID"=>$_GET['id']))->select();
		if(!empty($res3)){
			foreach($res3 AS $k => $v){
				$member .= $v['UserName'].',';
			}
		}
		$res4 = $this->field("RewardContent,ID")->where(array("ProjectID"=>$_GET['id']))->find();
		$res5 = $this->alias('a')->field("b.*,(SELECT UserName FROM user WHERE ID = b.UserID) AS UserName")->join("LEFT JOIN projectsummary_labor AS b ON a.ID = b.PSID")->where(array("a.ProjectID"=>$_GET['id']))->order("ID DESC")->select();
		$res6 = M("projectsummary_comm")->field("*")->where(array("PSID"=>$res4['ID']))->select();
		
	
		foreach($res5 AS $k => $v){
			$laborArr[$i]['JSON'][0]['UserName'] = $v['UserName'];
			$laborArr[$i]['JSON'][1]['Role'] = $v['Role'];
			$laborArr[$i]['JSON'][2]['WorkContent'] = $v['WorkContent'];
			$laborArr[$i]['JSON'][3]['Eva'] = $v['Eva'];
			$laborArr[$i]['JSON'][4]['UserID'] = $v['UserID'];
			$laborArr[$i]['JSON'][5]['LID'] = $v['ID'];
			$i++;
		}

		foreach($res6 AS $k => $v){
			$comArr[$j]['JSON'][0]['ProjectStage'] = $v['ProjectStage'];
			$comArr[$j]['JSON'][1]['WorkDuty'] = $v['WorkDuty'];
			$comArr[$j]['JSON'][2]['Date'] = $v['StartDate'].' ~ '.$v['EndDate'];
			$comArr[$j]['JSON'][3]['Description'] = $v['Description'];
			$comArr[$j]['JSON'][4]['ProjectMember'] = $v['ProjectMember'];
			$comArr[$j]['JSON'][5]['CustomMember'] = $v['CustomMember'];
			$comArr[$j]['JSON'][6]['OutputContent'] = $v['OutputContent'];
			$comArr[$j]['JSON'][7]['Memo'] = $v['Memo'];
			$comArr[$j]['JSON'][8]['LID'] = $v['ID'];
			$j++;
		}


		$dataArr['projectName'] = $res2['Name'];
		$dataArr['projectCycle'] = $projectCycle;
		$dataArr['ChargeUserName'] = $res2['UserName'];
		$dataArr['projectMember'] = substr($member,0,-1);
		$dataArr['Intro'] = $res2['Intro'];
		$dataArr['ImportantPoint'] = $res2['ImportantPoint'];
		$dataArr['RewardContent'] = $res4['RewardContent'];
		$dataArr['SummaryID'] = $res4['ID'];
		$dataArr['data'][0]['Labor'] = $laborArr;
		$dataArr['data'][1]['Comm'] = $comArr;
		
		//var_dump($dataArr);
		return $dataArr;
	}
}

?>				